package com.nsoft.html5npub.utils;

import com.nsoft.html5npub.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

@SuppressLint("SetJavaScriptEnabled")
@SuppressWarnings("deprecation")
public class ExWebView extends WebView {

	private Context context;
	private MyWebChromeClient webChromeClient;
	private View customView;
	private CustomViewCallback customViewCallback;
	
	
	private boolean isHighPriority=false;
	private RelativeLayout parentView;
	
	public void setParentView(View _pView) {
		this.parentView=(RelativeLayout)_pView;
	}
	
	public boolean isHighPriority() {
		return isHighPriority;
	}
	public void setHighPriority(boolean isHighPriority) {
		this.isHighPriority=isHighPriority;
	}


	public ExWebView(Context context,AttributeSet attrs) {
		super(context,attrs);
		init(context);
	}

	
	private void init(Context c) {
		this.context=c;
		
		webChromeClient=new MyWebChromeClient();
	    setWebChromeClient(webChromeClient);
	    setWebViewClient(new ExWebViewClient());
	       
	    WebSettings settings=getSettings();
	    if (settings!=null){
		    settings.setAllowFileAccess(true);
		    settings.setLoadWithOverviewMode(true);
			settings.setUseWideViewPort(true);
			settings.setBuiltInZoomControls(true);
			settings.setJavaScriptEnabled(true);
			settings.setPluginsEnabled(true);
			settings.setRenderPriority(isHighPriority?
					RenderPriority.HIGH:RenderPriority.LOW);
				    
			settings.setDomStorageEnabled(true);
		    
			if (Build.VERSION.SDK_INT>=
					Build.VERSION_CODES.HONEYCOMB){
				settings.setDisplayZoomControls(false);
			}
			
			this.setHorizontalScrollBarEnabled(false);
			this.setVerticalScrollBarEnabled(false);
			
			/**
			this.setBackgroundColor(Color.BLACK);
			*/
			this.setBackgroundColor(0);
	    }
	}
	
    private void hideCustomView() {
		webChromeClient.onHideCustomView();
	}
    
    @Override
    protected void onDetachedFromWindow() {
    	if (customView!=null){
    		this.hideCustomView();
    	}
    	super.onDetachedFromWindow();
    }

	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if (keyCode==android.view.KeyEvent.KEYCODE_BACK) {
            if (customView!=null) {
            	this.hideCustomView();
            	return true;
            }
    	}
    	return super.onKeyDown(keyCode, event);
    }
    
    private class MyWebChromeClient extends WebChromeClient {
		private Bitmap defaultVideoPoster;
    	
		@Override
		public boolean onConsoleMessage(
				ConsoleMessage consoleMessage) {
			Log.e("ExWebView.MyWebChromeClient.onConsoleMessage()",
					consoleMessage.toString());
			return true;
		}
		
    	@Override
		public void onShowCustomView(View view, 
					CustomViewCallback callback){
    		
	        if (customView!=null) {
	            callback.onCustomViewHidden();
	            return;
	        }
	        
	        customView=openCustomVideoView(view);
	        parentView.addView(customView);
	        customViewCallback=callback;
		}
		
		@Override
		public void onHideCustomView() {
			if (customView==null)
				return;
			
			customView.setVisibility(View.GONE);
			parentView.removeView(customView);
			customView=null;
			if (customViewCallback!=null){
				customViewCallback.onCustomViewHidden();
			}
		}
		
		@Override
		public Bitmap getDefaultVideoPoster() {
			if (defaultVideoPoster==null) {
				defaultVideoPoster=BitmapFactory.decodeResource(
					getResources(),R.drawable.ic_default_video);
		    }
			return defaultVideoPoster;
		}
		
		private View openCustomVideoView(View videoView){
			LayoutInflater inflater=(LayoutInflater)
					ExWebView.this.context.
						getSystemService(Context.
								LAYOUT_INFLATER_SERVICE);
			
			View view=inflater.inflate(R.layout.video_custom_view,
						ExWebView.this.parentView,false);
			
			ImageButton closeVideoBtn=(ImageButton)view.
						findViewById(R.id.closeVideoBtn);
			closeVideoBtn.setOnClickListener(
						new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onHideCustomView();
				}
			});
			
			LinearLayout videoLayout=(LinearLayout)view.
							findViewById(R.id.videoFrameLayout);
			videoLayout.addView(videoView);
			
			return view;
		}
		
		/**
		@Override
		public View getVideoLoadingProgressView() {
	        if (videoProgressView==null) {
	            LayoutInflater inflater=LayoutInflater.from(context);
	            videoProgressView=inflater.inflate(
	            		R.layout.video_loading_view,null);
	        }
	        return videoProgressView; 
		}
    	*/
		
		/**
		@Override
		public void onReceivedTitle(WebView view,String title) {
			((Activity)context).setTitle(title);
		}
		*/

		/**
     	@Override
     	public void onProgressChanged(WebView view,int newProgress) {
     		((Activity)context).getWindow().setFeatureInt(
     				Window.FEATURE_PROGRESS,newProgress*100);
     	}
     	*/
        
     	/**
     	@Override
     	public void onGeolocationPermissionsShowPrompt(String origin, 
     					GeolocationPermissions.Callback callback){
     		callback.invoke(origin,true,false);
     	}
     	*/
    }
}
